/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { describe, expect, test } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';
import { resolve } from 'path';

import { CompileModeEnum } from '../../../src/enum/compile-mode-enum';
import { initByStageEmptyApp, initExecuteCommand, runHvigor } from '../common/common';
import { ModuleBuildProfileModifier } from '../common/modifier/module-build-profile-modifier';
import { Probes } from '../common/probes';

describe('delete test', () => {
  test.concurrent('delete native config', async () => {
    await initByStageEmptyApp(async (dir) => {
      const commands = initExecuteCommand({
        mode: 'module',
        module: 'entry',
        tasks: 'default@ProcessLibs'
      });
      const result = runHvigor(dir, commands);
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      const probe = new Probes(dir, resolve(dir, 'entry'));
      expect(fse.readdirSync(path.resolve(probe.getIntermediatesProcessLibs(), 'arm64-v8a'))).toContain('libentry.so');

      // 删除native配置
      const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'entry');
      moduleBuildProfileModifier.setBuildOption({ compileMode: CompileModeEnum.ES_MODULE }).outputSync();

      // 重新build
      const buildWithoutCleanResult = runHvigor(dir, commands);
      expect(buildWithoutCleanResult.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      // 产物中的so被删除了
      expect(fse.readdirSync(probe.getIntermediatesProcessLibs())).toHaveLength(0);
    }, {
      enableNative: true
    });
  });
});
